package Json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;

/**
* @author jie
* @version 创建时间：2018年6月13日 上午9:19:41
* 测试Expose接口
* 
* //不导出实体中没有用@Expose注解的属性 excludeFieldsWithoutExposeAnnotation
* Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
		
* 只有声明了 Expose 注解且 serialize 值为 true 的字段才能被序列化，
* 只有声明了 Expose 注解且 deserialize 值为 true 的字段才能被反序列化
* 
* 
*/
public class ExposeTest {
	public static void main(String[] args) {
		//不导出实体中没有用@Expose注解的属性 excludeFieldsWithoutExposeAnnotation
		Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
		User2 user = new User2("A", "B", "C", "D", "E","F");
		System.out.println();
		//序列化
		System.err.println(gson.toJson(user));

		String json = "{\"a\":\"A\",\"b\":\"B\",\"c\":\"C\",\"d\":\"D\",\"e\":\"E\",\"f\":\"F\"}";
		user = gson.fromJson(json, User2.class);
		System.out.println();
		System.out.println(user.toString());
	}
}

class UserTwo{
	
}

class User2 {

	@Expose(serialize = true, deserialize = true) // 序列化和反序列化都生效
	private String a;

	@Expose(serialize = false, deserialize = true) // 序列化时不生效，反序列化时生效
	private String b;

	@Expose(serialize = true, deserialize = false) // 序列化时生效，反序列化时不生效
	private String c;

	@Expose(serialize = false, deserialize = false) // 序列化和反序列化都不生效，和不写注解一样
	private String d;

	@Expose
	private String e;
	
	private String f;

	public User2(String a, String b, String c, String d, String e,String f) {
		this.a = a;
		this.b = b;
		this.c = c;
		this.d = d;
		this.e = e;
		this.f = f;
	}

	@Override
	public String toString() {
		return "User [a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + ", e=" + e + ", f=" + f + "]";
	}

	

}